const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');


var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart: [
     
    ],
    totalprice: 0,
    hasList: false,
    selectAll: false,
    delet:false,
    hasLogin:false,
    arr:[],
    shopCart:[]
  },
  pay(){
    let cart = this.data.cart;
    this.data.att = []
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].checked) {
        cart[i].checked=true
        // let df={
        //   checked:cart[i].checked,
        //   commodityId: cart[i].commodityId,
        //   id: cart[i].id,
        // }
        this.data.shopCart.push(cart[i])
      }
     
    }
    console.log(this.data.att)
    let that = this
    util.request(api.UpdateChecked, 
       that.data.shopCart
    , 'post', {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function (res) {
      console.log(res)
      if(res.code==200){
        wx.navigateTo({
          url: '../Order/Order',
        })
      }else{
        return false
      }
    });
   
  },
// 加载购物车
  selct(){
    let that = this
    util.request(api.Select,{}, 'post', {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        that.setData({
          cart: res.data.cartList
        });
        if (that.data.cart.length == 0) {
          that.setData({
            hasList: false
          })
        } else {
          that.setData({
            hasList: true
          })
        }
        that.total();
      });
    
  },
  goLogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  // 添加数量
  add(e) {
    console.log(e.target.dataset.id)
    let that=this
    const index = e.currentTarget.dataset.index
    let cart = that.data.cart;
    let number = cart[index].number;
    number = number + 1;
    cart[index].number = number;
    that.setData({
      cart: cart
    })
    that.total()
    util.request(api.AppendCartNum,
      {
        id: that.data.cart[index].id,
        num: number,
        type:''
      }
      , 'post', {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token'),
      }).then(function (res) {
       if(res.msg=="库存不足"){
         util.showErrorToast('库存不足');
       }
        that.selct()
      });
  },
  // 减少商品
  reduce(e) {
    console.log(e.target.dataset.id)
    const index = e.currentTarget.dataset.index;
    let cart = this.data.cart;
    let number = cart[index].number;
    if (number <= 1) {
      return false
    }
    number = number - 1;
    cart[index].number = number;
    this.setData({
      cart: cart
    })
    this.total()
    let that=this
    util.request(api.AppendCartNum,
      {
        id: that.data.cart[index].id,
        num: number,
        type: ''
      }
      , 'post', {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token'),
      }).then(function (res) {
        if (res.msg == "库存不足") {
          util.showErrorToast('库存不足');
        }
        that.selct()
      });
  },
  // 总价
  total() {
    let cart = this.data.cart;
    let total = 0;
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].checked) {
        total += cart[i].number * cart[i].price
      }
    }
    this.setData({
      cart: cart,
      totalprice: total.toFixed(2)
    })
  },

  // 选择

  select(e) {
    console.log(e)
    const index = e.currentTarget.dataset.index;
    let cart = this.data.cart;
    const checked = cart[index].checked;
    cart[index].checked = !checked;
    this.setData({
      cart: cart
    })
    this.total();
  },
  // 全选
  selectall(e) {
    let selectAll = this.data.selectAll;
    selectAll = !selectAll;
    let cart = this.data.cart;
    for (let i = 0; i < cart.length; i++) {
      cart[i].checked = selectAll
    }
    this.setData({
      selectAll: selectAll,
      cart: cart
    })
    this.total();
  },
// 删除
delete(){
  let that=this
  let cart = that.data.cart;
  let total = 0;
  for (let i = 0; i < cart.length; i++) {
    if (cart[i].checked) {
      that.data.arr.push(cart[i].id)
      console.log(that.data.arr)
    }
    }
  util.request(api.DeleteCart,
    that.data.arr
    , 'post', {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function (res) {
      console.log(res)
      that.setData({
        arr:[]
      })
      that.selct()
    });
  // console.log(this.data.cart.length)
  // const index = e.currentTarget.dataset.index;
  // let cart = this.data.cart;
  // cart.splice(index,1);
  // this.setData({
  //   cart:cart
  // })
  this.total();
  if(cart.length==0){
    this.setData({
      hasList:false
    })
  } else{
    this.setData({
      hasList: true
    })
  }
},
//编辑
edit(){
  let selectAll = this.data.selectAll;
  selectAll = false;
  let cart = this.data.cart;
  for (let i = 0; i < cart.length; i++) {
    cart[i].checked = selectAll
  }
  this.setData({
    selectAll: selectAll,
    cart: cart,
    delet: true
  })
  this.total();
},
//完成
  ok() {
    let selectAll = this.data.selectAll;
    selectAll = true;
    let cart = this.data.cart;
    for (let i = 0; i < cart.length; i++) {
      cart[i].checked = selectAll
    }
    this.setData({
      selectAll: selectAll,
      cart: cart,
      delet: false
    })
    this.total();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.selct()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.selct()
    console.log('sss')
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})